unit uListagemCompra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uListagemPadrao, Grids, DBGrids, RXDBCtrl, StdCtrls, Buttons,
  ExtCtrls, DB, IBODataset;

type
  TfListCompra = class(TfListagemPadrao)
    CompraConsulta: TIBOQuery;
    DataSource1: TDataSource;
    CompraConsultaCODIGO: TIntegerField;
    CompraConsultaNUM_NOTA: TIntegerField;
    CompraConsultaFORNECEDOR_FK: TIntegerField;
    CompraConsultaCONDICAO_PGTO_FK: TIntegerField;
    CompraConsultaDATA_COMPRA: TDateField;
    CompraConsultaDATA_ENTRADA: TDateField;
    CompraConsultaVALOR_TOTAL: TBCDField;
    CompraConsultaSITUACAO: TStringField;
    CompraConsultaADIC_PERC: TBCDField;
    CompraConsultaADIC_VAL: TBCDField;
    CompraConsultaDESC_PERC: TBCDField;
    CompraConsultaDESC_VAL: TBCDField;
    CompraConsultaSUB_TOTAL: TBCDField;
    CompraConsultaNOME: TStringField;
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fListCompra: TfListCompra;

implementation

uses uCadCompra, uDm, uPrincipal;

{$R *.dfm}

procedure TfListCompra.SpeedButton1Click(Sender: TObject);
begin
  inherited;
  try
    dm.qryCondPgto.Open;
    Application.CreateForm(TfCadCompra, fCadCompra);
    fCadCompra.dbCondicao.Clear;
    dm.qryCompra.close;
    dm.qryCompra.Open;
    dm.qryCompra.Insert;
    if dm.configuracoesFECHAMENTO_CAIXA.AsString = 'True' then
      dm.qryCompraDATA_ENTRADA.AsDateTime := dm.qryFechamentoPROXIMA_ABERTURA.AsDateTime
    else
      dm.qryCompraDATA_ENTRADA.AsDateTime := Date;
    dm.qryCompraSITUACAO.AsString := 'N';
    dm.qryCompraItens.Close;
    fCadCompra.grid.RowCount := 0;
    Visible := false;
    fCadCompra.showmodal;
  finally
    FreeAndNil(fCadCompra);
    Visible := true;
    CompraConsulta.Close;
    CompraConsulta.Open;
    dm.qryCompra.Close;
  end;
end;

procedure TfListCompra.FormShow(Sender: TObject);
begin
  inherited;
  dm.qryCompra.Open;
  CompraConsulta.Open;
end;

procedure TfListCompra.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  inherited;
  if odd(CompraConsulta.RecNo) then
    DBGrid1.Canvas.Brush.Color:= fPrincipal.linhaEscura
  else
    DBGrid1.Canvas.Brush.Color:= fPrincipal.linhaClara;

  TDbGrid(Sender).Canvas.font.Color:= clBlack;
    if gdSelected in State then
      with (Sender as TDBGrid).Canvas do
        begin
          Brush.Color := fPrincipal.linhaSelecionada;
          FillRect(Rect);
          Font.Style := [fsbold]
        end;

  TDbGrid(Sender).DefaultDrawDataCell(Rect, TDbGrid(Sender).columns[datacol].field, State);
end;

procedure TfListCompra.SpeedButton2Click(Sender: TObject);
begin
  inherited;
  try
    dm.qryCondPgto.Open;
    Application.CreateForm(TfCadCompra, fCadCompra);
    dm.qryCompra.close;
    dm.qryCompra.SQL.Clear;
    dm.qryCompra.SQL.Add('select * from compra');
    dm.qryCompra.SQL.Add('where compra.codigo = '+CompraConsultaCODIGO.AsString);
    dm.qryCompra.Open;

    dm.exec.close;
    dm.exec.SQL.Clear;
    dm.exec.SQL.Add('select * from financeiro');
    dm.exec.SQL.Add('where financeiro.compra_fk = :DOCUMENTO');
    dm.exec.ParamByName('DOCUMENTO').Value := dm.qryCompraCODIGO.AsInteger;
    dm.exec.ExecSQL;
    if not (dm.exec.IsEmpty) then
    begin
      fCadCompra.btnFaturar.Visible := false;
      fCadCompra.SpeedButton6.Visible := false;
      fCadCompra.adicionaParcelas;
    end;

    dm.qryCondPgto.Open;
    dm.qryCondPgto.Locate('CODIGO',dm.qryCompraCONDICAO_PGTO_FK.AsString,[]);
    fCadCompra.Edit2.Text := dm.qryCondPgtoDESCRICAO.AsString;

    fCadCompra.dbFornecedorExit(sender);
    fCadCompra.atualizaItens;
    fCadCompra.dbDescValor.Value := dm.qryCompraDESC_VAL.Value;
    fCadCompra.dbDescPerc.Value  := dm.qryCompraDESC_PERC.Value;
    fCadCompra.dbAdicPerc.Value  := dm.qryCompraADIC_PERC.Value;
    fCadCompra.dbAdicValor.Value := dm.qryCompraADIC_VAL.Value;
    Visible := false;
    fCadCompra.showmodal;
  finally
    FreeAndNil(fCadCompra);
    Visible := true;
    CompraConsulta.Close;
    CompraConsulta.Open;
    dm.qryCompra.Close;
  end;
end;

procedure TfListCompra.SpeedButton3Click(Sender: TObject);
begin
  inherited;
  if MessageDlg('Deseja excluir essa nota?',mtConfirmation,[mbYes,mbNo],1) = mrYes then
  begin
    dm.exec.Close;
    dm.exec.SQL.Clear;
    dm.exec.SQL.Add('delete from compra');
    dm.exec.SQL.Add('where compra.codigo = :COD_compra');
    dm.exec.ParamByName('COD_compra').Value := CompraConsultaCODIGO.Value;
    dm.exec.ExecSQL;
    CompraConsulta.close;
    CompraConsulta.Open;
  end;
end;

end.
